package com.toutiao.action;

import java.util.List;

import com.base.dialect.PaginationSupport;
import com.base.dao.model.Ret;
import com.base.dao.model.Grid;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.apache.commons.lang.StringUtils;

import com.base.util.JSONUtils;
import com.base.web.annotation.LoginFilter;

import org.springframework.beans.factory.annotation.Autowired;

import com.base.action.CoreController;
import com.item.service.UserService;
import com.toutiao.dao.model.News;
import com.toutiao.dao.model.NewsCate;
import com.toutiao.dao.model.NewsCateExample;
import com.toutiao.dao.model.NewsCollectExample;
import com.toutiao.dao.model.NewsComment;
import com.toutiao.dao.model.NewsCommentExample;
import com.toutiao.dao.model.NewsCommentReplyExample;
import com.toutiao.dao.model.NewsExample;
import com.toutiao.dao.model.NewsPraise;
import com.toutiao.dao.model.NewsPraiseExample;
import com.toutiao.daoEx.model.NewsCommentEx;
import com.toutiao.daoEx.model.NewsCommentReplyEx;
import com.toutiao.service.NewsCateService;
import com.toutiao.service.NewsCollectService;
import com.toutiao.service.NewsCommentReplyService;
import com.toutiao.service.NewsCommentService;
import com.toutiao.service.NewsPraiseService;
import com.toutiao.service.NewsService;

@RequestMapping("newsCate")
@Controller
@LoginFilter
public class NewsCateController extends CoreController{
	
	@Autowired
	private NewsCateService newsCateService;
	@Autowired
	private NewsService newsService;
	@Autowired
	private NewsCollectService newsCollectService;
	@Autowired
	private NewsCommentService newsCommentService;
	@Autowired
	private NewsCommentReplyService newsCommentReplyService;
	@Autowired
	private NewsPraiseService newsPraiseService;

	@RequestMapping("/list")
	@ResponseBody
	public String list(Integer page, Integer rows, String name){
		PaginationSupport.byPage(page, rows);
		NewsCateExample example = new NewsCateExample();
		if(null ==name){
			name = "";
		}
		example.createCriteria().andNameLike("%"+name+"%");
		example.setOrderByClause("sort_order asc");
		List<NewsCate> list = newsCateService.selectByExample(example);
		return JSONUtils.serialize(new Grid(PaginationSupport.getContext().getTotalCount(), list));
	}

	@RequestMapping("/save")
	@ResponseBody
	public String save(NewsCate newsCate){
		if (newsCate.getId() == null){
			newsCateService.insert(newsCate);
		}else{
			newsCateService.updateByPrimaryKeySelective(newsCate);
		}
		return JSONUtils.serialize(new Ret(0));
	}

	@RequestMapping("/findById")
	@ResponseBody
	public String find(Integer id){
		NewsCate newsCate = newsCateService.selectByPrimaryKey(id);
		return JSONUtils.serialize(newsCate);
	}

	@RequestMapping("/del")
	@ResponseBody
	public String del(String id){
		String[] ids = id.split(",");
		for (String str : ids){
			
			NewsExample example = new NewsExample();
			example.createCriteria().andCateIdEqualTo(Integer.parseInt(str));
			List<News> newsList = newsService.selectByExample(example);
			
			NewsCollectExample example2 = new NewsCollectExample();
			
			NewsCommentExample example3 = new NewsCommentExample();
			NewsPraiseExample example5 = new NewsPraiseExample();
			
			NewsCommentReplyExample example4 = new NewsCommentReplyExample();
			
			for(News news : newsList){	
				example2.clear();
				example2.createCriteria().andNewsIdEqualTo(news.getId());
				//新闻收藏删除
				newsCollectService.deleteByExample(example2);
				//查看新闻 评论
				example3.clear();
				example3.createCriteria().andNewsIdEqualTo(news.getId());
				
				List<NewsComment> lists = newsCommentService.selectByExample(example3);
				for(NewsComment newsComments : lists){
					example5.clear();
					example5.createCriteria().andFidEqualTo(newsComments.getId());
					List<NewsPraise> newsPraiseList = newsPraiseService.selectByExample(example5);
					for(NewsPraise newsPraise : newsPraiseList){
						if(newsPraise.getType() == 2){
							//删除回复中点赞的
							newsPraiseService.deleteByExample(example5);
						}
						//删除回复中评论点赞的
						newsPraiseService.deleteByExample(example5);
					}
					
					example4.clear();
					example4.createCriteria().andCommentIdEqualTo(newsComments.getId());
					//删除回复
					newsCommentReplyService.deleteByExample(example4);
					//删除评论
					newsCommentService.deleteByPrimaryKey(newsComments.getId());
				}
				//新闻删除
				newsService.deleteByPrimaryKey(news.getId());	
			}
						
			newsCateService.deleteByPrimaryKey(Integer.parseInt(str));
		}
		return JSONUtils.serialize(new Ret(0));
	}

	@RequestMapping("/listName")
	@ResponseBody
	public String listName(){
		NewsCateExample example = new NewsCateExample();
		List<NewsCate> list = newsCateService.selectByExample(example);
		return JSONUtils.serialize(list);
	}
}